package com.lmds.sprindemo1.aop;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


/**
 * Demo for CGLib simple usage
 */
@Component
public class CGLibDemo {
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CGLibDemo.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("Before invoke "+ method);
                Object res = methodProxy.invokeSuper(o, objects);
                System.out.println("After invoke "+ method);
                return res;
            }
        });
        CGLibDemo proxyDemo = (CGLibDemo) enhancer.create();
        proxyDemo.test();

        System.out.println(proxyDemo);

    }

    public void test(){
        System.out.println("CGLibDemo run test()");
    }

    public void test2(){
        System.out.println("CGLibDemo run test2()");
    }
}
